﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Shapes;
using WinFormStore.Common;
using WinFormStore.Erp;
using WinFormStore.ScannerSerialPro;
using WinFormStore.实体库;
using WinFormStore.数据层;
using WinFormStore.通信层;

namespace WinFormStore
{
    public partial class FrmLogin : Form
    {

        //配置文件路径
        public string _path = Application.StartupPath + "\\scanner.ini";
        //配置服务对象
        public ScannerInfoService _infoService = new ScannerInfoService();
        //配置实体对象
        public ScannerInfo _scannerInfo = null;
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            if (this.txtacc.Text.Equals("") || this.txtpwd.Text.Equals(""))
            {
                MessageBox.Show("用户名和密码不能为空！");
                return;
            }
            string sql = "select * from SalesMan where Mobile=@Mobile and Pwd=@Pwd";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("Mobile", this.txtacc.Text);
            DBHelper.SetParameter("Pwd", this.txtpwd.Text);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery();
            if (dt.Rows.Count == 1)
            {
                LoginConfig.SalesManID = int.Parse(dt.Rows[0]["SalesmanID"].ToString());
                LoginConfig.SalesManName = dt.Rows[0]["SalesManName"].ToString();
                LoginConfig.Mobile = dt.Rows[0]["Mobile"].ToString();
                LoginConfig.Role = dt.Rows[0]["Role"].ToString();
                if (dt.Rows[0]["Role"].ToString().Equals("店长"))
                {
                    MessageBox.Show("店长!");
                    FrmMain frmMain = new FrmMain(_path, _infoService, _scannerInfo);
                    frmMain.Show();
                    this.Hide();

                }
                else if (dt.Rows[0]["Role"].ToString().Equals("收银员"))
                {
                    MessageBox.Show("收银员!");
                    FrmCash frmCash = new FrmCash(_path, _infoService, _scannerInfo);
                    this.Hide();
                    frmCash.ShowDialog();
                }
                else
                {
                    FrmMain frmMain = new FrmMain();
                    frmMain.Show();
                    this.Hide();
                }
            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
        }




        private void FrmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                this._scannerInfo = _infoService.GetScannerInfoFromPath(_path);
            }
            catch (Exception ex)
            {

                MessageBox.Show("配置信息加载失败:" + ex.Message, "读取配置");
                return;
            }


        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

        public void ShowFrom(Form f)
        {
            this.panel2.Controls.Clear();
            //将该子窗体设置成非顶级控件
            f.TopLevel = false;
            //将该子窗体的边框去掉
            f.FormBorderStyle = FormBorderStyle.None;
            //设置子窗体随容器大小自动调整
            f.Dock = DockStyle.Fill;
            //设置mdi父容器为当前窗口
            f.Parent = this.panel2;
            //子窗体显示
            f.Show();
        }
        private void lblForgetPwd_Click(object sender, EventArgs e)
        {
            FrmForget frmForget = new FrmForget();
            ShowFrom(frmForget);
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            FrmPort frmPort = new FrmPort(_path, _scannerInfo, _infoService);
            frmPort.ShowDialog();
        }

        private void lblenroll_Click(object sender, EventArgs e)
        {
            FrmRegister frmRegister = new FrmRegister();
            ShowFrom(frmRegister);
        }
        private void btnSetShow()
        {
            string sql = "select Role from SalesMan where Mobile=@Mobile";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("Mobile", this.txtacc.Text);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery();
            if (dt != null && dt.Rows.Count > 0)
            {
                string role = dt.Rows[0]["Role"].ToString();
                if (role == "" || (role != "店长" && role != "导购员" && role != "收银员"))
                {

                    this.btnSet.Hide();
                }
                else
                {
                    this.btnSet.Show();
                }
            }
            else
            {
                this.btnSet.Hide();
            }
        }

        private void txtacc_TextChanged(object sender, EventArgs e)
        {
            btnSetShow();
        }

        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
    }
}
